<template>
  <div class="enterprise-list">
    <el-card class="info">
      <el-form class="el-form" :model="tableData[0]">
        <el-form-item label="企业编号" label-width="80px">
          <el-input v-model.trim="tableData.eid" style="width=20px" />
        </el-form-item>
        <el-form-item label="企业名称" label-width="80px">
          <el-input v-model.trim="tableData.name" />
        </el-form-item>
        <el-form-item label="创建者" label-width="80px">
          <el-input v-model.trim="tableData.username" />
        </el-form-item>
        <el-form-item label="状态" label-width="80px">
          <el-select v-model.trim="tableData.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="clear">清除</el-button>
        <el-button type="primary" @click="add">+ 新增企业</el-button>
      </el-form>
    </el-card>
    <el-card class="detail">
      <el-table :data="tableData">
        <!-- 真实序号处理 -->
        <el-table-column label="序号" width="80px">
          <template v-slot="{ $index }">
            {{ (page.page - 1) * page.limit + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="企业编号" prop="eid" />
        <el-table-column label="企业名称" prop="name" />
        <el-table-column label="创建者" prop="username" />
        <el-table-column label="状态" prop="status">
          <template v-slot="{ row }">
            {{ row.status === 1 ? '禁用' : '启用' }}
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <!-- row是当前行的数据 -->
          <template v-slot="{ row }">
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" @click="changeStatus(row.id)">
              {{ row.status === 0 ? '禁用' : '启用' }}
            </el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination" style="text-align: right">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 10, 20, 30, 40]"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增弹框 -->
    <Add ref="add" @getData="getData" />
  </div>
</template>

<script>
import jsCookie from 'js-cookie'
import {
  enterpriseList,
  enterpriseRemove,
  enterpriseStatus
} from '@/api/enterpriseList'
import Add from './components/Add.vue'
export default {
  components: { Add },
  data() {
    return {
      list: [],
      eid: '',
      page: {
        page: 1,
        limit: +jsCookie.get('enterprise-size') || 10,
        total: 10
      },
      options: [
        { value: '选项1', label: '禁用' },
        { value: '选项2', label: '启用' }
      ],
      value: '',
      tableData: [
        {
          index: '1',
          username: '', // 用户名
          eid: '', // 企业编号
          name: '', // 名称
          short_name: '',
          intro: '',
          remark: '',
          status: '', // 状态
          id: ''
        }
      ]
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取企业列表信息
    async getData() {
      const res = await enterpriseList({
        ...this.tableData,
        page: this.page.page,
        limit: this.page.limit
      })
      // 存储企业列表信息
      this.tableData = res.data.items
      // console.log(this.tableData)
      this.page.total = +res.data.pagination.total
      console.log('企业列表数据', res)
    },
    //  页容量改变
    handleSizeChange(size) {
      // console.log('当前页容量', size)
      this.page.limit = size
      // 页容量选项持久化
      jsCookie.set('enterprise-size', size)
      // 调用接口
      this.getData()
    },
    // 页码改变
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
      // console.log('当前页码', page)
    },
    // 新增企业
    add() {
      // 打开弹框组件
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    // 删除企业
    // 点击删除获取对应的id值
    del(id) {
      this.$confirm('确定删除吗?', '温馨提示')
        .then(async() => {
          await enterpriseRemove({ id })
          this.$message.success('删除成功')
          if (this.page.page > 1 && this.tableData.length <= 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑企业
    edit(row) {
      // 打开弹框组件
      this.$refs.add.isShow = true
      // 修改新增弹框组件内的mode
      this.$refs.add.mode = 'edit'
      // 数据回显
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },

    // 改变状态
    async changeStatus(id) {
      const res = await enterpriseStatus({ id })
      this.$message.success('修改状态成功')
      this.getData()
      console.log(res)
    },

    // 搜索
    search() {
      this.page.page = 1
      this.getData()
    },
    // 清除列表
    clear() {
      this.form = {
        eid: '',
        name: '',
        username: ''
      }
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.enterprise-list {
  .pagination {
    display: flex;
    justify-content: center;
    margin-top: 10px;
  }
  padding: 15px;
  .el-form {
    display: flex;
    .el-button{
      height: 40px;
      margin-left: 10px;
    }
  }
}
</style>
